package com.zjkj.mp.controller.channelManage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zjkj.mp.service.channel.ChannelService;
import com.zjkj.mp.utils.Global;
import com.zjkj.mp.utils.PaginatedResult;
import com.zjkj.mp.utils.Toolkit;
import com.zjkj.mp.vo.channel.MpChannel;

/**
 * 
 * 通道列表控制器
 *
 * @author 陈晨
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2013-6-9</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
@Controller
public class ChannelTypeController {

	@Resource(name = "channelServiceImpl")
	private ChannelService channelService;

	/******************************* 通道列表 ***********************************/
	
	/** 
	 * 通道分页列表
	 * @return
	 */
	@RequestMapping("/showAllList.do")
	public String showAllList(HttpServletRequest req) {
		String pageNoStr = req.getParameter("pageNo");
		String tdId = req.getParameter("tdId");
		int pageNo = 1;
		PaginatedResult result;
		if(pageNoStr != null){
			pageNo = Integer.parseInt(pageNoStr);
		}
		if(tdId != null){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("tdId", tdId);
			result = this.channelService.getMpChannelPageList(pageNo, Global.PAGESIZE, map);
		}else{
			result = this.channelService.getMpChannelPageList(pageNo, Global.PAGESIZE, null);
		}
		req.setAttribute("result", result);		
		return "channel/channelList";
	}
	
	/**
	 * 根据标识查询通道列表
	 * @return
	 */
	@RequestMapping("/showAllListById.do")
	public String showAllListById(HttpServletRequest req) {
		String tdId = req.getParameter("tdId");
		List<MpChannel> list = channelService.getMpChannelList(tdId);
		req.setAttribute("channelList", list);
		return "channel/channelList";
	}
	
	/**
	 * 根据标识编辑通道信息
	 * @return
	 */
	@RequestMapping("/editChannel.do")
	public String editChannelInfoById(HttpServletRequest req) {
		String tdId = req.getParameter("tdId");
		List<MpChannel> list = channelService.getMpChannelList(tdId);
		MpChannel mc = list.get(0);
		req.setAttribute("mpChannel", mc);
		return "channel/editChannel";
	}
	
	/**
	 * 更新通道信息
	 * @return
	 */
	@RequestMapping("/updChannel.do")
	public String updChannelInfoById(@ModelAttribute("channel") MpChannel channel, HttpServletRequest req,
			HttpServletResponse res) {
		String tdId = req.getParameter("tdId");
		Toolkit tool = new Toolkit();
		//设置通道时间跟状态
		channel.setTdzt(1);
		channel.setPubtime(tool.getSystemDate());
		channel.setTdId(Integer.parseInt(tdId));
		int num = channelService.updMpChannel(channel);
		if(num>0){
			req.setAttribute("info", "更新成功");
		} else {
			req.setAttribute("info", "更新失败");
		}
		return "channel/channelList";
	}
	
	/**
	 * 根据标识删除通道信息
	 * @return
	 */
	@RequestMapping("/delChannel.do")
	public String delChannelById(HttpServletRequest req) {
		String tdId = req.getParameter("tdId");
		int num = channelService.delMpChannel(Integer.parseInt(tdId));
		if(num>0){
			req.setAttribute("info", "删除成功");
		} else {
			req.setAttribute("info", "删除失败");
		}
		return "channel/channelList";
	}
	
	/**
	 * 根据标识启动通道列表
	 * @return
	 */
	@RequestMapping("/flagChannel.do")
	public String flagChannelById(HttpServletRequest req) {
		String tdId = req.getParameter("tdId");
		String tdzt = req.getParameter("tdzt");
		int flag = Integer.parseInt(tdzt);
		int num;
		if(flag==1){
			num = channelService.flagChannel(Integer.parseInt(tdId), 0);
		}else{
			num = channelService.flagChannel(Integer.parseInt(tdId), 1);
		}
		if(num>0){
			req.setAttribute("info", "修改成功");
		} else {
			req.setAttribute("info", "修改失败");
		}
		return "channel/channelList";
	}
	
}
